package ${package}.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;

import cn.cx.bean.constant.factory.PageFactory;
import cn.cx.bean.entity.shop.Cart;
import cn.cx.bean.vo.front.Rets;
import cn.cx.bean.vo.query.SearchFilter;
import cn.cx.utils.factory.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ${package}.convert.${ClassName}Convert;
import ${package}.service.${ClassName}Service;
import ${package}.vo.${ClassName}VO;
import cn.cx.bean.entity.shop.${ClassName};
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

/**
* ${tableComment}
*
* @author ${author} ${email}
* @since ${version} ${date}
*/
@RestController
@RequestMapping("${moduleName}/${functionName}")
@Tag(name="${tableComment}")
@AllArgsConstructor
public class ${ClassName}Controller {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private final ${ClassName}Service ${className}Service;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('${moduleName}:${functionName}:page')")
    public Object page(@RequestBody Map<String, String> map){

        Page<${ClassName}> page = new PageFactory<${ClassName}>().defaultPage();
        for(String key : map.keySet()){
            page.addFilter(key, SearchFilter.Operator.LIKE, map.get(key));
        }
        page = ${className}Service.queryPage(page);

        //Page<${ClassName}VO> page1 = new PageFactory<${ClassName}VO>().defaultPage();
        //page1.setRecords(${ClassName}Convert.INSTANCE.convertList(page.getRecords()));
        return Rets.success(page);
    }


    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('${moduleName}:${functionName}:info')")
    public Object get(@PathVariable("id") String id){
        ${ClassName} entity = ${className}Service.get(id);

        return Rets.success(${ClassName}Convert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('${moduleName}:${functionName}:save')")
    public Object save(@RequestBody ${ClassName}VO vo){
        ${className}Service.insert(${ClassName}Convert.INSTANCE.convert(vo));

        return Rets.success();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('${moduleName}:${functionName}:update')")
    public Object update(@RequestBody @Valid ${ClassName}VO vo){
        ${className}Service.update(${ClassName}Convert.INSTANCE.convert(vo));

        return Rets.success();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('${moduleName}:${functionName}:delete')")
    public Object delete(@RequestBody List<String> idList){
        ${className}Service.delete(idList);

        return Rets.success();
    }
}